﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace CorporationWars
{
    [Serializable]
    public class UILabel : UIZone
    {
        public Color MouseDownColor { get; set; }
        public Color BGColor { get; set; }
        public string _text { get; set; }

        public bool Selected { get; set; }
        public Color SelectedColor { get; set; }

        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                CalcPos();
            }
        }
        
        public string FontAsset { get; set; }

        public Vector2 TextPos { get; set; }

        public override Vector2 Position
        {
            get { return base.Position; }

            set
            {
                base.Position = value;
                CalcPos();
            }
        }

        [NonSerialized]
        SpriteFont font = null;

        protected Vector2 _padding { get; set; }
        public Vector2 padding
        {
            get { return _padding; }
            set
            {
                _padding = value;
                CalcPos();
            }
        }


        public UILabel(Game1 game,string text, Vector2 position, string fontAsset, Color color, Color mousedownColor, Vector2 padding) : base(game) 
        {
            BGColor = Color.White;
            OpacityTolerance = .125f;
            Game = game;
            TextureAsset = "Textures/halfWhite";
            Text = text;
            FontAsset = fontAsset;
            Color = color;
            MouseDownColor = mousedownColor;
            Position = position;

            SelectedColor = Color.Silver;

            this.padding = padding;
            // Calc the text position.
            CalcPos();
        }

        protected void CalcPos()
        {
            if (Game != null && !string.IsNullOrEmpty(FontAsset))
            {
                font = Game.Content.Load<SpriteFont>(FontAsset);

                Vector2 textDim = font.MeasureString(Text);

                if(Width == 0)
                    Width = (int)textDim.X + (int)padding.X * 2;

                Height = (int)textDim.Y + (int)padding.Y * 2;

                TextPos = Position + padding;
            }
        }


        public override void Draw(GameTime gameTime)
        {
            if (!IsVisible)
                return;;

            if (Selected)
            {
                spriteBatch.Draw(Game.Content.Load<Texture2D>(TextureAsset), renderRectangle, SelectedColor );
            }

            if (!IsMouseOver)
            {
                spriteBatch.DrawString(font, Text, TextPos - new Vector2(1, -1), Color.Black);
                spriteBatch.DrawString(font, Text, TextPos, Color);
            }
            else
            {
                if (!IsMouseDown)
                {
                    spriteBatch.Draw(Game.Content.Load<Texture2D>(TextureAsset), renderRectangle, BGColor);
                    spriteBatch.DrawString(font, Text, TextPos - new Vector2(1, -1), Color.Black);
                    spriteBatch.DrawString(font, Text, TextPos, Color);
                    DrawToolTip(gameTime);
                }
                else
                {
                    spriteBatch.Draw(Game.Content.Load<Texture2D>(TextureAsset), renderRectangle, MouseDownColor);
                    spriteBatch.DrawString(font, Text, TextPos - new Vector2(1, -1), Color.Black);
                    spriteBatch.DrawString(font, Text, TextPos, MouseDownColor);
                }
            }
        }
    }
}
